package com.blustreaksw.cyclecoach.userManagment;

import com.blustreaksw.cyclecoach.R;
import com.blustreaksw.cyclecoach.UserManagementActivity;
import com.blustreaksw.cyclecoach.UserProfileActivity;
import com.blustreaksw.cyclecoach.MainActivity;
import com.blustreaksw.cyclecoach.R.id;
import com.blustreaksw.cyclecoach.R.layout;
import com.blustreaksw.cyclecoach.btle.BtleDeviceType;
import com.blustreaksw.cyclecoach.data.UserProfile;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.NavUtils;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

public class Fragment_Wizard_SensorConfiguration extends Fragment {
	
	Button mAddHrmSensorButton;
	TextView mHrmDeviceNameTextView;
	TextView mHrmDeviceAddressTextView;
	
	Button mAddCscSensorButton;
	TextView mCscDeviceNameTextView;
	TextView mCscDeviceAddressTextView;
	
	Button mAddPowerSensorButton;
	TextView mPowerDeviceNameTextView;
	TextView mPowerDeviceAddressTextView;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setHasOptionsMenu(true);
		setRetainInstance(true);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View v = inflater.inflate(R.layout.fragment_wizard_edit_bike_profile,
				container, false);
		
		
		getActivity().setTitle("Heart Rate Monitor");
		
		mAddHrmSensorButton = (Button)v.findViewById(R.id.wizard_createUser_addHrmSensor);		
		mAddHrmSensorButton.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				
				if(getUserProfileFromActivity().getHeartRateMonitorAddress().equals("")) {
					mAddHrmSensorButton.setText("Remove Sensor");
					Fragment fragment;
	    			if((fragment = getActivity().getSupportFragmentManager().
	    					findFragmentByTag(UserManagementActivity.FRAGMENT_DEVICE_SEARCH)) == null) {
	    				Bundle args = new Bundle();
	    				args.putInt(Fragment_BtleDeviceSearch.EXTRA_BTLE_DEVICE_TYPE, BtleDeviceType.HEART_RATE_MONITOR.getValue());
	    				fragment = new Fragment_BtleDeviceSearch();
	    				fragment.setArguments(args);
	    				
	    				if(((UserProfileActivity)getActivity()).isWizardActivity() == false) {
	    					((MainActivity)getActivity()).setCentralNavigationStartViewPosition(2);
	    				}
	    				
	    				getActivity().getSupportFragmentManager().beginTransaction()
							.replace(R.id.fragmentContainer, fragment, UserManagementActivity.FRAGMENT_DEVICE_SEARCH)
							.addToBackStack(null)
							.commit();
	    			}
				} else {
					if(((UserProfileActivity)getActivity()).isWizardActivity() == false) {
						((MainActivity)getActivity()).disconnectDevice(mHrmDeviceAddressTextView.getText().toString());
					}
					// Clear the HRM device fields in the UserProfile
					getUserProfileFromActivity().setHeartRateMonitorName("");
					getUserProfileFromActivity().setHeartRateMonitorAddress("");
					getUserProfileFromActivity().updateUserFromCurrentProfileValues(getActivity());
					
					// Reset the view.
					mAddHrmSensorButton.setText("Add Sensor");
					mHrmDeviceNameTextView.setText(getUserProfileFromActivity().getHeartRateMonitorName());
					mHrmDeviceAddressTextView.setText(getUserProfileFromActivity().getHeartRateMonitorAddress());
				}
				
			}
			
		});
		
		mAddCscSensorButton = (Button)v.findViewById(R.id.wizard_createUser_addCscSensor);
		mAddCscSensorButton.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				
				if(getUserProfileFromActivity().getCyclingSpeedCadenceSensorAddress().equals("")) {
					mAddCscSensorButton.setText("Remove Sensor");
					Fragment fragment;
	    			if((fragment = getActivity().getSupportFragmentManager().
	    					findFragmentByTag(UserManagementActivity.FRAGMENT_DEVICE_SEARCH)) == null) {
	    				Bundle args = new Bundle();
	    				args.putInt(Fragment_BtleDeviceSearch.EXTRA_BTLE_DEVICE_TYPE, BtleDeviceType.CYCLING_SPEED_CADENCE_SENSOR.getValue());
	    				fragment = new Fragment_BtleDeviceSearch();
	    				fragment.setArguments(args);
	    				
	    				getActivity().getSupportFragmentManager().beginTransaction()
							.replace(R.id.fragmentContainer, fragment, UserManagementActivity.FRAGMENT_SENSOR_CONFIGURATION)
							.addToBackStack(null)
							.commit();
	    			}
				} else {
					
					if(((UserProfileActivity)getActivity()).isWizardActivity() == false) {
						((MainActivity)getActivity()).disconnectDevice(mCscDeviceAddressTextView.getText().toString());
					}
					
					// Clear the CSC device fields in the UserProfile
					getUserProfileFromActivity().setCyclingSpeedCadenceSensorName("");
					getUserProfileFromActivity().setCyclingSpeedCadenceSensorAddress("");
					getUserProfileFromActivity().updateUserFromCurrentProfileValues(getActivity());
					
					// Reset the view.
					mAddCscSensorButton.setText("Add Sensor");
					mCscDeviceNameTextView.setText(getUserProfileFromActivity().getCyclingSpeedCadenceSensorName());
					mCscDeviceAddressTextView.setText(getUserProfileFromActivity().getCyclingSpeedCadenceSensorAddress());
				}
				
			}
			
		});
		
		mAddPowerSensorButton = (Button)v.findViewById(R.id.wizard_createUser_addPowerMeterSensor);
		mAddPowerSensorButton.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				
				if(getUserProfileFromActivity().getCyclingPowerMeterAddress().equals("")) {
					mAddPowerSensorButton.setText("Remove Sensor");
					Fragment fragment;
	    			if((fragment = getActivity().getSupportFragmentManager().
	    					findFragmentByTag(UserManagementActivity.FRAGMENT_DEVICE_SEARCH)) == null) {
	    				Bundle args = new Bundle();
	    				args.putInt(Fragment_BtleDeviceSearch.EXTRA_BTLE_DEVICE_TYPE, BtleDeviceType.CYCLING_POWER_METER.getValue());
	    				fragment = new Fragment_BtleDeviceSearch();
	    				fragment.setArguments(args);
	    				
	    				getActivity().getSupportFragmentManager().beginTransaction()
							.replace(R.id.fragmentContainer, fragment, UserManagementActivity.FRAGMENT_SENSOR_CONFIGURATION)
							.addToBackStack(null)
							.commit();
	    			}
				} else {
					if(((UserProfileActivity)getActivity()).isWizardActivity() == false) {
						((MainActivity)getActivity()).disconnectDevice(mPowerDeviceAddressTextView.getText().toString());
					}
					
					// Clear the POWER device fields in the UserProfile
					getUserProfileFromActivity().setCyclingPowerMeterName("");
					getUserProfileFromActivity().setCyclingPowerMeterAddress("");
					getUserProfileFromActivity().updateUserFromCurrentProfileValues(getActivity());
					
					// Reset the view.
					mAddPowerSensorButton.setText("Add Sensor");
					mPowerDeviceNameTextView.setText(getUserProfileFromActivity().getCyclingPowerMeterName());
					mPowerDeviceAddressTextView.setText(getUserProfileFromActivity().getCyclingPowerMeterAddress());
				}
				
			}
			
		});
		
		mHrmDeviceNameTextView = (TextView)v.findViewById(R.id.wizard_createUser_hrm_deviceNameTextView);
		mHrmDeviceAddressTextView = (TextView)v.findViewById(R.id.wizard_createUser_hrm_addressTextView);
		mHrmDeviceNameTextView.setText(getUserProfileFromActivity().getHeartRateMonitorName());
		mHrmDeviceAddressTextView.setText(getUserProfileFromActivity().getHeartRateMonitorAddress());
		
		mCscDeviceNameTextView = (TextView)v.findViewById(R.id.wizard_createUser_csc_deviceNameTextView);
		mCscDeviceAddressTextView = (TextView)v.findViewById(R.id.wizard_createUser_csc_addressTextView);
		mCscDeviceNameTextView.setText(getUserProfileFromActivity().getCyclingSpeedCadenceSensorName());
		mCscDeviceAddressTextView.setText(getUserProfileFromActivity().getCyclingSpeedCadenceSensorAddress());
		
		mPowerDeviceNameTextView = (TextView)v.findViewById(R.id.wizard_createUser_powerMeter_deviceNameTextView);
		mPowerDeviceAddressTextView = (TextView)v.findViewById(R.id.wizard_createUser_powerMeter_addressTextView);
		mPowerDeviceNameTextView.setText(getUserProfileFromActivity().getCyclingPowerMeterName());
		mPowerDeviceAddressTextView.setText(getUserProfileFromActivity().getCyclingPowerMeterAddress());
		
		
		if(getUserProfileFromActivity().getHeartRateMonitorAddress().equals("")) {
			mAddHrmSensorButton.setText("Add Sensor");
		} else {
			mAddHrmSensorButton.setText("Remove Sensor");
		}
		
		if(getUserProfileFromActivity().getCyclingSpeedCadenceSensorAddress().equals("")) {
			mAddCscSensorButton.setText("Add Sensor");
		} else {
			mAddCscSensorButton.setText("Remove Sensor");
		}
		
		if(getUserProfileFromActivity().getCyclingPowerMeterAddress().equals("")) {
			mAddPowerSensorButton.setText("Add Sensor");
		} else {
			mAddPowerSensorButton.setText("Remove Sensor");
		}
		return v;
	}
	
	@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
		if(((UserProfileActivity)getActivity()).isWizardActivity()) {
		// Show only cancel, previous and next buttons.
		menu.findItem(R.id.action_createUserWizard_cancelButton).setVisible(true);
		menu.findItem(R.id.action_createUserWizard_prevButton).setVisible(true);
		menu.findItem(R.id.action_createUserWizard_nextButton).setVisible(true);
		
		
		// Hidden options
		menu.findItem(R.id.action_createUserWizard_newUser).setVisible(false);
		menu.findItem(R.id.action_createUserWizard_saveButton).setVisible(false);
		menu.findItem(R.id.action_createUserWizard_restartDeviceScan).setVisible(false);
		menu.findItem(R.id.scanning_indicator)
        	.setActionView(R.layout.progress_indicator).setVisible(false);
		}
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
    	switch(item.getItemId()) {

    		case R.id.action_createUserWizard_cancelButton:
    			getActivity().getSupportFragmentManager().popBackStack();
    			getActivity().getSupportFragmentManager().popBackStackImmediate();
    			return true;
    			
    		case R.id.action_createUserWizard_prevButton:
    			// Return to the Create_User_1 fragment
    			getActivity().getSupportFragmentManager().popBackStackImmediate();
    			return true;
    			
    		case R.id.action_createUserWizard_nextButton:
    			
    			// Transition to the Create_User_3 fragment
    			Fragment fragment;
    			if((fragment = getActivity().getSupportFragmentManager().
    					findFragmentByTag(UserManagementActivity.FRAGMENT_DATA_EXPORT)) == null) {
    				fragment = new Fragment_Wizard_DataExport();
    			}
    			
    			getActivity().getSupportFragmentManager().beginTransaction()
					.replace(R.id.fragmentContainer,  fragment, UserManagementActivity.FRAGMENT_DATA_EXPORT)
					.addToBackStack(null)
					.commit();
    			return true;
    		
    		// Hidden from the user.
    		case R.id.action_createUserWizard_newUser:
    		case R.id.action_createUserWizard_restartDeviceScan:
    		case R.id.action_createUserWizard_saveButton:   		
    		default:
    			return super.onOptionsItemSelected(item); 
    	}
	}
	
	private UserProfile getUserProfileFromActivity() { return ((UserProfileActivity)getActivity()).getUserProfile(); }

}
